package com.cis.web.controller.api;

import com.cis.common.config.RuoYiConfig;
import com.cis.common.config.ServerConfig;
import com.cis.common.core.controller.BaseController;
import com.cis.common.core.domain.AjaxResult;
import com.cis.common.utils.BmpUtils;
import com.cis.common.utils.ByteUtils;
import com.cis.common.utils.file.FileUploadUtils;
import com.cis.education.domain.EduCheckin;
import com.cis.education.domain.EduCheckinStudent;
import com.cis.education.domain.EduFingerPrint;
import com.cis.education.domain.EduStudent;
import com.cis.education.service.*;
import com.sun.xml.internal.bind.v2.TODO;
import io.swagger.models.auth.In;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/api/finger")
@ResponseBody
public class FingerApiController extends BaseController {

    @Autowired
    IEduCheckinService checkinService;

    @Autowired
    IEduTeacherService teacherService;

    @Autowired
    IEduCourseService courseService;

    @Autowired
    IEduStudentService studentService;

    @Autowired
    IEduCheckinStudentService checkinStudentService;

    @Autowired
    IEduClassroomService classroomService;

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private IEduFingerPrintService fingerPrintService;


    /**
     * 查询正在进行中的签到
     */
    @GetMapping("/ongoing/checkins")
    public AjaxResult getOngoingCheckins(){
        EduCheckin eduCheckin = new EduCheckin();
        eduCheckin.setStatus(0);
        List<EduCheckin> eduCheckins = checkinService.selectEduCheckinList(eduCheckin);
        for (EduCheckin checkin : eduCheckins) {
            checkin.setTeacher(teacherService.selectEduTeacherById(checkin.getTeacherId()));
            checkin.setCourse(courseService.selectEduCourseById(checkin.getCourseId()));
            checkin.setClassroom(classroomService.selectEduClassroomById(checkin.getClassroomId()));
        }
        return AjaxResult.success(eduCheckins);
    }

    /**
     * 根据签到ID查询学生名单
     */
    @GetMapping("/checkin/{checkinId}/students")
    public AjaxResult getStudentListByCheckinId(@PathVariable Integer checkinId){
        EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
        eduCheckinStudent.setCheckinId(checkinId);
        List<EduCheckinStudent> eduCheckinStudents = checkinStudentService.selectEduCheckinStudentList(eduCheckinStudent);
        List<EduStudent> result = new ArrayList<>();
        for (EduCheckinStudent checkinStudent : eduCheckinStudents) {
            EduStudent eduStudent = studentService.selectEduStudentById(checkinStudent.getStudentId());
            //查询当前学生签到状态
            System.out.println(checkinStudent);
            eduStudent.setCheckinStatus(checkinStudent.getStatus());
            result.add(eduStudent);
        }
        if (result.isEmpty()){
            return AjaxResult.error();
        }
        return AjaxResult.success(result);
    }

    /**
     * 根据学生ID查询学生信息
     */
    @GetMapping("/student/{id}")
    public AjaxResult getStudentById(@PathVariable String id){
        EduStudent eduStudent = studentService.selectEduStudentById(id);
        if(eduStudent != null){
            eduStudent.setFingerPrint(fingerPrintService.selectEduFingerPrintByStudentId(id));
            return AjaxResult.success(eduStudent);
        }
        return AjaxResult.error();
    }

    /**
     * 上传指纹信息
     */
    @PostMapping("/fingerprint/upload")
    public AjaxResult uploadFingerprint(EduFingerPrint data, MultipartFile imageFile){
        String fileName = "";
        String url = "";
        //保存指纹图片
        try {
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            fileName = FileUploadUtils.upload(filePath, imageFile);
            url = serverConfig.getUrl() + fileName;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.error("上传指纹信息失败！");
        }
        EduFingerPrint eduFingerPrint = new EduFingerPrint();
        //查询当前学生是否已经存在指纹信息
        eduFingerPrint.setStudentId(data.getStudentId());
        List<EduFingerPrint> eduFingerPrints = fingerPrintService.selectEduFingerPrintList(eduFingerPrint);
        if (eduFingerPrints.isEmpty()){
            //新建学生指纹信息
            eduFingerPrint.setFlashId(data.getFlashId());
            eduFingerPrint.setImg(url);
            fingerPrintService.insertEduFingerPrint(eduFingerPrint);
            return AjaxResult.success("成功上传指纹信息");
        }else {
            eduFingerPrint.setId(eduFingerPrints.get(0).getId());
            eduFingerPrint.setFlashId(data.getFlashId());
            eduFingerPrint.setImg(url);
            fingerPrintService.updateEduFingerPrint(eduFingerPrint);
            return AjaxResult.success("成功上传指纹信息");
        }
    }

    /**
     * 检查签到
     */
    @GetMapping("/checkin/{checkinId}/finger/{flashId}")
    public AjaxResult checkinByFlashId(@PathVariable Integer checkinId, @PathVariable Integer flashId){
        //TODO 通过FlashID获取学生信息
        EduFingerPrint eduFingerPrint = new EduFingerPrint();
        eduFingerPrint.setFlashId(flashId);
        List<EduFingerPrint> eduFingerPrints = fingerPrintService.selectEduFingerPrintList(eduFingerPrint);
        if (eduFingerPrints.isEmpty()){
            return AjaxResult.error("签到失败");
        }
        //TODO 查询当前学生签到信息
        EduCheckinStudent eduCheckinStudent = new EduCheckinStudent();
        eduCheckinStudent.setCheckinId(checkinId);
        eduCheckinStudent.setStudentId(eduFingerPrints.get(0).getStudentId());
        List<EduCheckinStudent> eduCheckinStudents = checkinStudentService.selectEduCheckinStudentList(eduCheckinStudent);
        if (eduCheckinStudents.isEmpty()){
            return AjaxResult.error("签到失败");
        }
        if (eduCheckinStudents.get(0).getStatus() == 1){
            return AjaxResult.error("您已签到！");
        }
        eduCheckinStudents.get(0).setStatus(1);
        checkinStudentService.updateEduCheckinStudent(eduCheckinStudents.get(0));
        return AjaxResult.success(studentService.selectEduStudentById(eduCheckinStudents.get(0).getStudentId()));
    }
}
